<?

include($_SERVER['DOCUMENT_ROOT'].'/includes/functions.inc.php');
include($_SERVER['DOCUMENT_ROOT'].'/portals/config.php');
include($_SERVER['DOCUMENT_ROOT'].'/groups/database.php');

require_once($_SERVER['DOCUMENT_ROOT'].'/geograph/feedcreator.class.php');

$format = "RSS1.0";
$rssfile = "cache/recent.rss";

$valid_formats=array('RSS0.91','RSS1.0','RSS2.0','MBOX','OPML','ATOM','ATOM0.3','HTML','JS','PHP');

if (!empty($_GET['format']) && in_array($_GET['format'], $valid_formats))
{
        $format=$_GET['format'];
	$rssfile = "cache/recent.$format.rss";
}



$sql = array();
$sql['columns'] = '*';
$sql['tables'] = array();
$sql['tables'][] = "portal";
$sql['wheres'] = array();

$sql['wheres'][] = "status_id = 1";
$sql['wheres'][] = "public = 'Y'";


$sql['order'] = "portal_id DESC";

$sql['limit'] = 20;

	$query = sqlMakeQuery($sql);


	$data = getAssoc($query);

	if ($data) {
		$rss = new UniversalFeedCreator();
		//if (empty($_GET['refresh']))
		//        $rss->useCached($format,$rssfile);

		$rss->title = 'Recent Geograph Portals';
		$rss->link = "http://{$_SERVER['HTTP_HOST']}/portals/";

		foreach ($data as $id => $row) {

			$count = count_active($id);

	                if ($count) {

			 	$item = new FeedItem();

			        $item->title = $row['title'];
				 $item->link = "http://{$row['domain']}.portal.geographs.org/";
				 $item->guid = $item->link;

				$item->description = $row['description']. " ($count images)";
				$item->date = strtotime($row['created']);

				$rss->addItem($item);
			
			}

		}

		$rss->saveFeed($format,$rssfile);

		
	} else {
		die("nothing to see here");
	}

